package au.chao.client;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import org.apache.log4j.Logger;

import au.chao.message.builder.MessageBuilder;
import au.chao.message.builder.TextMessageBuilder;
import au.chao.message.sender.SocketMessageSender;
import au.chao.setting.ServerConfig;

public class JIMClient {
	private static final Logger log = Logger.getLogger(JIMClient.class);
	Socket socket = null;
	String id;

	public JIMClient(int i) {
		id = "client Id:" + i;
	}

	public void connect(String server) {
		try {
			socket = new Socket(server, ServerConfig.DEFAULT_SERVER_PORT_NUM);
			MessageBuilder msgBuilder = new TextMessageBuilder();
			msgBuilder.buildHeader(1, 2);
			msgBuilder.buildContent("hello this is chao:" + id);
			new SocketMessageSender(socket, msgBuilder.getMessage()).send();
		} catch (UnknownHostException e) {
			log.error("unknown host", e);
		} catch (IOException e) {
			log.error("Send message failed", e);
		}
	}

	public static void main(String[] s) {
		for (int i = 0; i < 100; i++) {
			new JIMClient(i).connect("127.0.0.1");
		}
	}
}
